﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Text;
using System.IO;
using Nextel.Utils;

public class PushEnvio
{
    public static void subEnvioPush(String pCodUsuario, String pMensaje)
    {
        //Uri address = new Uri("http://solucionescorporativas.nextel.com.pe/servicio_push/push.ashx");

        //Uri address = new Uri("http://" + "201.175.36.212/servicio_push" + "/push.ashx");

        Uri address = new Uri("http://" + ConfigurationManager.AppSettings["PUSH_URL"].ToString() + "/push.ashx");


        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        ///String lsTelefono = DALBase.getTelefonoXUsuario(pCodUsuario, "USPH_TELEFONO_X_USUARIO");
        String lsNextel = DALBase.getTelefonoXUsuario(pCodUsuario, "USPH_NEXTEL_X_USUARIO");

        String lsPushKey = ConfigurationManager.AppSettings["PUSH_CODCLI"].ToString();

        StringBuilder data = new StringBuilder();
        //data.Append("nextel=" + HttpUtility.UrlEncode(Tarea.getTelefono(lsTelefono, 9)));
        data.Append("nextel=" + HttpUtility.UrlEncode(lsNextel));
        data.Append("&cliente=" + HttpUtility.UrlEncode(lsPushKey));
        data.Append("&mensaje=" + HttpUtility.UrlEncode(pMensaje));

        //data.Append("nextel=" + HttpUtility.UrlEncode("51*100*2327"));
        //data.Append("&cliente=" + HttpUtility.UrlEncode("1001"));
        //data.Append("&mensaje=" + HttpUtility.UrlEncode("prueba envio push"));
        //data.Append("&fecha=" + HttpUtility.UrlEncode("12/05/2011"));
        //data.Append("&hora=" + HttpUtility.UrlEncode("13:53"));


        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

        request.ContentLength = byteData.Length;

        try
        {

            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }


            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                String cadena = reader.ReadToEnd();
            }
        }
        catch (Exception ex)
        {
            BaseLN.registrarLog("ERROR PUSH" + ex);
        }
    }

    public String getTelefonoDigitos(String ptn)
    {
        String cadena = ptn;
        if (cadena != null)
        {
            if (cadena.Length < 9)//994001406
            { cadena = cadena.PadLeft(9, '0'); }

            cadena = cadena.Substring(cadena.Length - 9, 9);
        }
        return cadena;

    }
}

